#region using

using System.IO;
using System.Text;
using System.Xml.Serialization;

#endregion

namespace PS.Tools.Utils
{
    public static class XmlSerializationHelper<Class>
    {
        public static Class DeserializeFromFile(string fileName)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Open))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Class));
                return (Class)serializer.Deserialize(stream);
            }
        }

        public static void SerializeToFile(Class data, string fileName)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Class));
                serializer.Serialize(stream, data);
            }
        }

        public static Class Deserialize(string xmlString)
        {
            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Class));
                return (Class)serializer.Deserialize(stream);
            }

        }

        public static string Serialize(Class data)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Class));
                serializer.Serialize(stream, data);
                stream.Position = 0;
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                return Encoding.UTF8.GetString(bytes);
            }
        }
    }
}
